<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/12
 * Time: 07:09 AM
 */
class PHZ_Open
{

    public static $user_mopai_chk_info    = array();
    public static $pao_paizhe_is_chongpao = false;
    public static $pao_paizhe_uid         = 0;


    public static function __init_data(){
       self::$user_mopai_chk_info    = array();
       self::$pao_paizhe_is_chongpao = false;
       self::$pao_paizhe_uid         = 0;
    }

    public static function __mopai_all_user_chk( $room_id , $card_number , $mop_uid , $db_connect  ){
        //初始化数组(重要，很重要....)
        self::__init_data();
        //取出房间信息
        $room_info     = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //取出该房间所有人的牌面信息
        $card_info     = $db_connect->select('*')->from('tt_room_card')->where("jid = '". $room_info['room_js'] ."' and rid= '". $room_id ."' ")->orderByASC( array('id') )->query();
        $card_info     = PHZ_Card::__cardInfoUserSort( $card_info , $mop_uid );//按照摸牌人的顺序进行整理
        //摸牌者的牌面信息
        $mop_card_info = array();
        foreach ( $card_info as $key => $value ){
            if( $value['uid'] == $mop_uid ){
                $mop_card_info = $value;break;
            }
        }
        //进行第一次遍历，目的是为了初始化各用户数据数组
        foreach ( $card_info as $key => $value ){
            self::$user_mopai_chk_info[$value['uid']]['pao']       = 0;
            self::$user_mopai_chk_info[$value['uid']]['chong_pao'] = 0;
            self::$user_mopai_chk_info[$value['uid']]['peng']      = 0;//能跑则不会有碰
            self::$user_mopai_chk_info[$value['uid']]['chi']       = 0;//能跑则不会有吃 - 能胡也是有可以吃的
            self::$user_mopai_chk_info[$value['uid']]['hu']        = 0;//能跑，则不会有胡
        }
        /***************************************************************************************************************/
        /***************************************************************************************************************/
        //2017.12.26:新增自摸公式胡牌（即只要自己摸的，不管别人能不能跑；都是自己胡，因为不能过胡，所以不要管后面的执行了）
        $card_is_wei = false;
        $mop_card_wei = explode(",", $mop_card_info['card_wei'] );
        foreach ( $mop_card_wei as $key => $value ){if( $value == $card_number ){$card_is_wei = true;}}//该摸牌人是否畏过该牌
        if( $card_is_wei == false ) {
            $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi($room_id, $mop_uid);
            $canHu = PHZ_CanHu::__isCan_Hu_Pai($default_huxi, $mop_card_info['sp'], $card_number, $card_number , $room_info);//属于自摸
            if ($canHu['canHu'] == true) {
                $temp = array();
                $temp['pl'] = 31;$temp['card_number'] = $card_number;$temp['put_uid'] = 0;$temp['mop_uid'] = $mop_uid;
                PHZ_Log::__send_user_log($room_info, $mop_uid, $temp, $db_connect);
                //设置可胡牌状态
                PHZ_Room::__setStatus( $room_info , $mop_uid , 'hu' , 1 , $db_connect );
                //设置出牌权
                PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
                return false;
            }
        }

        //2018.02.02:修复胡牌顺序的问题；主要是因为跑胡（破跑胡）被优先了*****************************************************
        //2018.02.02:修复胡牌顺序的问题；主要是因为跑胡（破跑胡）被优先了*****************************************************
        $hupai_array = array();
        foreach ( $card_info as $key => $value ){
            if( $value['uid'] != $mop_uid ){
                #.获取默认胡息
                $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi( $room_id, $value['uid'] );
                #.第一步：是否能平胡
                $canHu        = PHZ_CanHu::__isCan_Hu_Pai( $default_huxi, $value['sp'], $card_number , -1 , $room_info );
                if ($canHu['canHu'] == true) {
                    $hupai_array[$value['uid']]['ping'] = true;
                }else{
                    $hupai_array[$value['uid']]['ping'] = false;
                }
                #.第二步：是否能跑胡
                $temp_sp = explode(",", $value['sp'] );$temp_find = 0;
                foreach ( $temp_sp as $key_1 => $value_1 ){if( $value_1 == $card_number ){$temp_find++;}}//手中是否有三个
                $card_is_wei = false;
                $card_wei    = explode(",", $value['card_wei'] );
                foreach ( $card_wei as $key_1 => $value_1 ){if( $value_1 == $card_number ){$card_is_wei = true;}}//该摸牌人是否畏过该牌
                $card_is_peng = false;
                $card_peng    = explode(",", $value['card_peng'] );
                foreach ( $card_peng as $key_1 => $value_1 ){if( $value_1 == $card_number ){$card_is_peng = true;}}//该用户是否碰过该牌
                if( $temp_find == 3 || $card_is_wei == true || $card_is_peng == true ){//如果有个人碰过或者畏过或者手中有三个（只会有一个这样的人）
                    if( $temp_find == 3 ) {//如果是手里三个，要加6-9；如果是碰或者畏；那么就加3-6
                        if ($card_number <= 10) {
                            $default_huxi = $default_huxi + 6;
                        } else {
                            $default_huxi = $default_huxi + 9;
                        }
                    }else if( $card_is_wei == true ){
                        if ($card_number <= 10) {
                            $default_huxi = $default_huxi + 3;
                        } else {
                            $default_huxi = $default_huxi + 3;
                        }
                    }else if( $card_is_peng == true ){
                        if ($card_number <= 10) {
                            $default_huxi = $default_huxi + 5;
                        } else {
                            $default_huxi = $default_huxi + 6;
                        }
                    }
                    $canHu = array();
                    $paiNumberArray         = PHZ_Card::__cardSpGETPaoSP__AND_TEMP_CARD_NUMBER( $value['sp'] );//进行临时牌解析与查询
                    if( $paiNumberArray[0] != "" && $paiNumberArray[1] != 0 ){
                        $canHu = PHZ_CanHu::__isCan_Hu_Pai( $default_huxi , $paiNumberArray[0] , $paiNumberArray[1] , -1 , $room_info );//跑胡判断
                    }else{
                        $canHu['canHu'] = false;
                    }
                    if ($canHu['canHu'] == true) {
                        $hupai_array[$value['uid']]['pao'] = true;
                    }else{
                        $hupai_array[$value['uid']]['pao'] = false;
                    }
                }
            }
        }
        $is_big_paohu = false;
        foreach ( $hupai_array as $key => $value ){//按照顺序进行胡牌
            if( isset( $value['pao'] ) && $value['pao'] == true ){//如果有跑胡，那么循环终止；一直到下面的跑胡后才进行胡牌判断
                $is_big_paohu = true;
                break;
            }
            if( $value['ping'] == true ){//是否有平胡
                $temp = array();
                $temp['pl']     = 33;$temp['card_number'] = $card_number;$temp['put_uid'] = 0;$temp['mop_uid'] = $mop_uid;
                $temp['hu_uid'] = $key;
                PHZ_Log::__send_user_log($room_info, $key , $temp, $db_connect);
                PHZ_Room::__setStatus( $room_info , $key , 'hu' , 1 , $db_connect );
                PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
                return false;
            }
        }
        //2018.02.02:修复胡牌顺序的问题；主要是因为跑胡（破跑胡）被优先了*****************************************************
        //2018.02.02:修复胡牌顺序的问题；主要是因为跑胡（破跑胡）被优先了*****************************************************

        //2017.12.26:修复BUG：（ 手中三个一样的字；还胡这个字；别人摸到，能不能胡？:可以胡 ） - 这里不需要验证摸牌人自己
        if( $is_big_paohu == false ) {
            foreach ( $card_info as $key => $value ){
                if( $value['uid'] != $mop_uid ){
                    //判断谁手里有三个
                    $temp_sp = explode(",", $value['sp'] );$temp_find = 0;
                    foreach ( $temp_sp as $key_1 => $value_1 ){if( $value_1 == $card_number ){$temp_find++;}}//查询累计个数
                    if( $temp_find == 3 ){
                        $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi( $room_id, $value['uid'] );
                        $canHu = PHZ_CanHu::__isCan_Hu_Pai( $default_huxi, $value['sp'], $card_number , -1 , $room_info );
                        if ($canHu['canHu'] == true) {
                            $temp = array();
                            $temp['pl']     = 32;$temp['card_number'] = $card_number;$temp['put_uid'] = 0;$temp['mop_uid'] = $mop_uid;
                            $temp['hu_uid'] = $value['uid'];
                            PHZ_Log::__send_user_log($room_info, $value['uid'] , $temp, $db_connect);
                            //设置可胡牌状态
                            PHZ_Room::__setStatus( $room_info , $value['uid'] , 'hu' , 1 , $db_connect );
                            //设置出牌权
                            PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
                            return false;
                        }
                        break;//只可能同时有一个人有，所以查到了就break
                    }
                }
            }
            //2017.12.26:修复BUG：（畏了一个字；还胡这个字；别人摸出这个字，能不能胡，算不算放炮:可以胡） - 这里不需要验证摸牌人自己
            foreach ( $card_info as $key => $value ){
                if( $value['uid'] != $mop_uid ){
                    $card_is_wei = false;
                    $card_wei    = explode(",", $value['card_wei'] );
                    foreach ( $card_wei as $key_1 => $value_1 ){if( $value_1 == $card_number ){$card_is_wei = true;}}//该摸牌人是否畏过该牌
                    if( $card_is_wei == true ){
                        $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi( $room_id, $value['uid'] );
                        $canHu = PHZ_CanHu::__isCan_Hu_Pai( $default_huxi, $value['sp'], $card_number , -1 , $room_info );
                        if ($canHu['canHu'] == true) {
                            $temp = array();
                            $temp['pl']     = 32;$temp['card_number'] = $card_number;$temp['put_uid'] = 0;$temp['mop_uid'] = $mop_uid;
                            $temp['hu_uid'] = $value['uid'];
                            PHZ_Log::__send_user_log($room_info, $value['uid'], $temp, $db_connect);
                            //设置可胡牌状态
                            PHZ_Room::__setStatus( $room_info , $value['uid'] , 'hu' , 1 , $db_connect );
                            //设置出牌权
                            PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
                            return false;
                        }
                        break;//只可能同时有一个人有，所以查到了就break
                    }
                }
            }
            //2018.01.14:修复BUG：碰了一个字；还胡这个字；提前进行破跑胡判断； - 这里不需要验证摸牌人自己
            foreach ( $card_info as $key => $value ){
                if( $value['uid'] != $mop_uid ){
                    $card_is_peng = false;
                    $card_peng    = explode(",", $value['card_peng'] );
                    foreach ( $card_peng as $key_1 => $value_1 ){if( $value_1 == $card_number ){$card_is_peng = true;}}//该用户是否碰过该牌
                    if( $card_is_peng == true ){
                        $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi( $room_id, $value['uid'] );
                        $canHu = PHZ_CanHu::__isCan_Hu_Pai( $default_huxi, $value['sp'], $card_number , -1 , $room_info );
                        if ($canHu['canHu'] == true) {
                            $temp = array();
                            $temp['pl']     = 33;$temp['card_number'] = $card_number;$temp['put_uid'] = 0;$temp['mop_uid'] = $mop_uid;
                            $temp['hu_uid'] = $value['uid'];
                            PHZ_Log::__send_user_log($room_info, $value['uid'], $temp, $db_connect);
                            //设置可胡牌状态
                            PHZ_Room::__setStatus( $room_info , $value['uid'] , 'hu' , 1 , $db_connect );
                            //设置出牌权
                            PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
                            return false;
                        }
                        break;//只可能同时有一个人有，所以查到了就break
                    }
                }
            }
        }
        /***************************************************************************************************************/
        /***************************************************************************************************************/
        //先检查所有人的跑，跟下跑能满足跑跟下跑的，不需要再检查吃跟碰； - 无论上面何种结果，都要检查胡；摸的人跟胡的人是同一个，为自摸；可能存在破跑胡的情况
        $is_pao = 0;
        foreach ( $card_info as $key => $value ){
            if( self::__all_user_chk_pao( $value , $card_number , $db_connect , $room_info ) == true ){
                $is_pao = 1;
            }
        }
        //设置当前房间摸牌人以及牌号 - 有除了摸牌人其他人能跑的情况下；清空
        if( $is_pao == 1 ) {
            PHZ_Room::__setThisMopCard($room_info['room_id'], 0, 0, true, $db_connect);
        }
        //检查所有人的碰的情况(当然上面的跑是不存在的) - 摸牌人自己是不能检查碰的
        foreach ( $card_info as $key => $value ){
            if( $is_pao == 0 && $value['uid'] != $mop_uid ){
                self::__all_user_chk_peng($value, $card_number, $db_connect, $room_info);
            }
        }
        //检查所有人的吃的情况(当然也是不能跑的情况下)
        foreach ( $card_info as $key => $value ){
            if( $is_pao == 0 ){
                self::__all_user_chk_chi( $value , $card_number , $mop_uid , $room_info , $db_connect );
            }
        }
        //检查所有人的胡的情况
        foreach ( $card_info as $key => $value ){
            //获取用户的基本息
            $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi( $room_id , $value['uid'] );
            //进行判胡操作(注意，跑胡的判断稍微有点不同) - 跑胡，需要用一个临时牌来作为胡牌的基础牌
            if( self::$user_mopai_chk_info[$value['uid']]['pao'] == 1 ){
                $paiNumberArray = PHZ_Card::__cardSpGETPaoSP__AND_TEMP_CARD_NUMBER( $value['sp'] );//进行临时牌解析与查询
                if( $paiNumberArray[0] != "" && $paiNumberArray[1] != 0 ){
                    if( $paiNumberArray[2] > 0 ){
                        $default_huxi = $default_huxi + $paiNumberArray[2];//如果有3张被去掉的牌，那么需要把这3张牌对应的息加上
                    }
                    $canHu = PHZ_CanHu::__isCan_Hu_Pai( $default_huxi , $paiNumberArray[0] , $paiNumberArray[1] , -1 , $room_info );//跑胡判断
                }else{
                    $canHu['canHu'] = false;
                }
            }else {
                $canHu = PHZ_CanHu::__isCan_Hu_Pai($default_huxi, $value['sp'], $card_number, -1 , $room_info );//普通胡判断
            }
            if( $canHu['canHu'] == true ){
                self::$user_mopai_chk_info[$value['uid']]['hu'] = 1;
            }
        }
        //是否有人能胡
        $is_hu     = false;
        foreach ( self::$user_mopai_chk_info as $key => $value ){
            if( $value['hu'] == 1 ){$is_hu = true;break;}
        }
        //如果没有人能胡，且存在跑的情况下；重跑则下家自动摸牌；首跑则等待跑牌者操作
        if( $is_hu == false && $is_pao == 1 ){
            if( self::$pao_paizhe_is_chongpao == true ){
                $room_info['debug_log'] = "next001:没有人能胡,且出现了重跑";
                PM_n::__index( $room_info , self::$pao_paizhe_uid , 0 , $db_connect );//重跑后；重跑者的下家摸牌
                return true;
            }
            return true;
        }
        //是否有人能进行胡，碰，吃的操作
        $is_caozuo = false;
        foreach ( self::$user_mopai_chk_info as $key => $value ){
            if( $value['hu'] == 1 || $value['peng'] == 1 || $value['chi'] == 1 ){
                $is_caozuo = true;break;
            }
        }
        //如果没有任何人能有操作，则下发牌废弃且转为自动下家摸牌
        if( $is_caozuo == false && $is_pao == 0 ){
            //给摸牌人增加一个bp操作
            $db_connect->update('tt_room_card')->cols(array('bp'=> $mop_card_info['bp'] . $card_number . ","  ))->where('id = '. $mop_card_info['id']  )->query();
            //给所有人下发该牌被丢弃，且没有任何人会要操作的日志
            $temp = array();
            $temp['pl']          = 19;//19为摸出的牌没有任何人能要，包括自己
            $temp['card_number'] = $card_number;
            $temp['put_uid']     = 0;
            $temp['mop_uid']     = 0;
            PHZ_Log::__send_user_log( $room_info , $mop_uid , $temp  , $db_connect );
            //设置出牌权
            PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
            //自动下家摸牌(摸牌者下家摸牌)
            $room_info['debug_log'] = "next002:该牌没有人有任何操作，自动下摸";
            PM_n::__index( $room_info , $mop_uid , 0 , $db_connect );
            return true;
        }
        //如果有人能进行胡碰吃的操作的时候,给每位玩家发送相应的记录log
        $hu_pai_uid   = array();
        $peng_pai_uid = array();
        $chi_pai_uid  = array();
        foreach ( self::$user_mopai_chk_info as $key => $value ){
            //将可以吃，碰，胡的人的信息记录到数组，方便传递给每一个人
            if( $value['hu']   == 1 ){array_push( $hu_pai_uid , $key );}
            if( $value['peng'] == 1 ){array_push( $peng_pai_uid , $key );}
            if( $value['chi']  == 1 ){array_push( $chi_pai_uid , $key );}
            //判断当前用户为第几个player
            $user_index = PHZ_Tools::__get_user_in_room_index( $room_info , $key );
            $p_index    = "p". $user_index ."_";
            //将每一个可以吃，碰，胡的用户，记录自己的is_XXX信息
            if( $value['hu']   == 1 ){
                $db_connect->update('tt_room')->cols(array( $p_index . "is_hu" => 1  ))->where('room_id = '. $room_id  )->query();
            }
            if( $value['peng'] == 1 ){
                $db_connect->update('tt_room')->cols(array( $p_index . "is_peng" => 1  ))->where('room_id = '. $room_id  )->query();
            }
            if( $value['chi']  == 1 ){
                $db_connect->update('tt_room')->cols(array( $p_index . "is_chi" => 1  ))->where('room_id = '. $room_id  )->query();
            }
        }
        foreach ( self::$user_mopai_chk_info as $key => $value ){
            self::$user_mopai_chk_info[$key]['hu_pai_uid']   = $hu_pai_uid;
            self::$user_mopai_chk_info[$key]['peng_pai_uid'] = $peng_pai_uid;
            self::$user_mopai_chk_info[$key]['chi_pai_uid']  = $chi_pai_uid;
        }
        /***************************************  经过了以上一系列的轮番检查   ******下面开始给每个人发送信息了  *************/
        if( self::$user_mopai_chk_info[$mop_uid]['hu'] == 1 ) {//摸牌人是否能胡牌
            $mopai_user_isHu = 1;
        }else{
            $mopai_user_isHu = 0;
        }
        foreach ( self::$user_mopai_chk_info as $key => $value ){
            $data = array();
            $data['log_rid']     = $room_info['room_id'];
            $data['log_jid']     = $room_info['room_js'];
            $data['log_uid']     = $mop_uid;//发信息默认为摸牌者
            $data['log_to_uid']  = $key;//收信息的人
            $data['log_md']      = 15;
            $temp = array();
            $temp['pl']          = 18;//18::摸牌后；摸牌者丢弃牌；同时有其他玩家满足碰，吃、胡要求（胡也可以是摸牌人自己）
            $temp['card_number'] = $card_number;//因为是摸牌后的操作，所以这里是明牌公式
            $temp['put_uid']     = 0;//全部人丧失出牌权
            $temp['mop_uid']     = 0;//全部人丧失摸牌权
            $temp['is_pao']       = $is_pao;//当前是否有人跑牌
            $temp['is_chongpao']  = self::$pao_paizhe_is_chongpao == true ? 1 : 0;//当前的跑牌是否为重跑
            $temp['pao_uid']      = self::$pao_paizhe_uid;//跑牌人的UID
            $temp['peng']         = $value['peng'];//是否能碰？
            $temp['chi']          = $value['chi'];//是否能吃？
            $temp['hu']           = $value['hu'];//是否能胡？
            $temp['hu_pai_uid']   = $value['hu_pai_uid'];//胡牌人组合
            $temp['peng_pai_uid'] = $value['peng_pai_uid'];//碰牌人组合
            $temp['chi_pai_uid']  = $value['chi_pai_uid'];//吃牌人组合
            if( $temp['chi']  == 1 ){
                $temp['chi_type'] = $value['chi_type'];//如果吃存在,则把吃的组合下发给用户
            }
            if( $temp['peng'] == 1 && $temp['chi'] == 1 ){//吃碰是否为同一人?
                $temp['chi_peng_one'] = 1;//吃碰为同一个人
            }else{
                $temp['chi_peng_one'] = 0;//吃碰非同一个人
            }
            #.2017.12.26:BUG:摸牌人也能胡的情况下；前端优先摸牌人胡
            $temp['mopai_hu']  = $mopai_user_isHu;
            $temp['mopai_uid'] = $mop_uid;
            #.2017.12.26:BUG:摸牌人也能胡的情况下；前端优先摸牌人胡
            $data['log_data']    = json_encode( $temp );
            $db_connect->insert( PHZ_Log::__gTn( $room_id ) )->cols( $data )->query();
            PHZ_Card::__update_MAX_HeaderID( $room_info , $key , $db_connect );//更新用户的当前最大可执行ID
        }
        //设置出牌权
        PHZ_Room::__setThisPutUser( $room_info['room_id'] , 0 , $db_connect );
        //返回
        return true;
    }

    /**
     * 检查这个牌是否有玩家能吃的进？吃只能检查本家或者下家
     * @param $user_card_info    用户牌面信息
     * @param $card_number       牌号
     * @param $mop_uid           摸牌者的ID
     * @param $room_info         房间信息
     * @return bool              返回true或者false
     */
    public static function __all_user_chk_chi( $user_card_info , $card_number , $mop_uid , $room_info , $db_connect  ){
        //设置临时用户ID
        $uid       = $user_card_info['uid'];
        //查询摸牌人下一个玩家的ID
        $next_uid  = PHZ_User::__get_user_Next_userID( $room_info , $mop_uid );
        //只能是摸牌人跟摸牌人的下家才需要进行吃的判断::条件::uid不等于摸牌人，又不等于摸牌人的下家
        if( $uid != $mop_uid && $uid != $next_uid ){
            return false;
        }
        //能执行到这里（只能是摸牌人跟）
        if( PHZ_User::__Eat_card_is_Out( $room_info , $uid , $card_number , $db_connect ) == false ){
            return false;
        }
        //查询吃牌 + 比牌信息
        $chi_pai   = PHZ_Chi_Bi_Pai::__chi_pai_bi_pai_chk( $card_number , $user_card_info['sp'] , $room_info );
        //存在吃牌的组合情况下
        if( $chi_pai != false && count($chi_pai) >= 1 ){
            self::$user_mopai_chk_info[$uid]['chi']      = 1;
            self::$user_mopai_chk_info[$uid]['chi_type'] = $chi_pai;
            return true;
        }else{
            return false;
        }
    }

    /**
     * 碰牌检查：检查是否有用户能碰牌；有的话，给能碰牌的用户下发碰牌的信息（理论上只有一个人能碰牌，不可能会有2个）
     * @param $user_card_info
     * @param $card_number
     * @param $db_connect
     * @param $room_info
     */
    public static function __all_user_chk_peng( $user_card_info , $card_number , $db_connect , $room_info ){
        //设置临时用户ID
        $uid = $user_card_info['uid'];
        //该牌是否过章
        if( PHZ_User::__Peng_card_is_Out( $room_info , $uid , $card_number , $db_connect ) == false ) {
            return false;
        }
        //检查用户数手牌是否能完成碰
        $user_card_info_sp = explode(",",$user_card_info['sp']);
        $find = 0;foreach ( $user_card_info_sp as $key => $value ){if( $value == $card_number ){$find++;}}//进行牌面检查（完成匹配则 + 1）
        if( $find == 2 ){//如果这里存在2个（不可能存在3个，存在3个就有问题了；因为3个的情况已经在上面进行了跑的判断了）
            self::$user_mopai_chk_info[$uid]['peng'] = 1;
            return true;
        }else{
            return false;
        }
    }

    /**
     * 跑牌检查：：这里是指的用户手里是否有3张这样的牌的检查;（场景：某用户摸牌20，这个时候刚好另一个玩家手中有3个20；构成本方法）
     * @param $user_card_info
     * @param $card_number
     */
    public static function __all_user_chk_pao( $user_card_info , $card_number , $db_connect , $room_info ){
        //设置临时用户ID
        $uid = $user_card_info['uid'];
        //检查用户是否能手牌起跑
        $user_card_info_sp = explode(",",$user_card_info['sp']);
        $find = 0;
        foreach ( $user_card_info_sp as $key => $value ){
            if( $value == $card_number ){$find++;}
        }
        if( $find != 3 ){//无法完成手里3张的检测；转由继续检测已经碰下来的牌
            $find = 0;
            //检查用户碰下去的牌，是否能完成跑牌
            $card_peng = explode(",", $user_card_info['card_peng'] );
            foreach ( $card_peng as $key => $value ){
                if( $value == $card_number ){
                    $pao_type = "peng";$find = 3;break;//找到碰就相当于找到了3张；
                }
            }
            //如果碰也没有；转由检测畏的牌
            if( $find != 3 ){
                $find = 0;
                $card_wei = explode(",", $user_card_info['card_wei'] );
                foreach ( $card_wei as $key => $value ){
                    if( $value == $card_number ){
                        $pao_type = "wei";$find     = 3;break;//找到了畏也相当于找到了3张牌
                    }
                }
            }
        }else{
            $pao_type = "shou";
        }
        /**************************************  上面的检查都完成了以后？？  **********************************************/
        if( $find == 3 ){//存在跑的情况
            self::$pao_paizhe_uid                   = $uid;//设置跑牌者UID
            self::$user_mopai_chk_info[$uid]['pao'] = 1;
            if( $user_card_info['ti_pao'] >= 1 ){self::$user_mopai_chk_info[$uid]['chong_pao'] = 1;}//是否重跑
            //根据不同的跑的情况执行更新操作:shou，删除sp，添加xp  peng | wei  ，替换xp
            if( $pao_type == "shou"  ) {
                foreach ($user_card_info_sp as $key => $value) {
                    if ($value == $card_number) {
                        unset($user_card_info_sp[$key]);
                    }
                }
                $update_sp = implode(",", $user_card_info_sp);
                $update_xp = $user_card_info['xp'] . $card_number . "_" . $card_number . "_" . $card_number . "_" . $card_number . ",";
                $db_connect->update('tt_room_card')->cols(array('xp'=> $update_xp ,'sp'=>$update_sp, 'ti_pao' => ($user_card_info['ti_pao']+1)   ))->where('id=' . $user_card_info['id'] )->query();
            }else{
                $update_xp = str_replace( $card_number . "_" . $card_number . "_" . $card_number , $card_number . "_" . $card_number . "_" . $card_number. "_" . $card_number , $user_card_info['xp'] );//三连变4连
                $db_connect->update('tt_room_card')->cols(array('xp'=> $update_xp , 'ti_pao' => ($user_card_info['ti_pao']+1)   ))->where('id=' . $user_card_info['id'] )->query();
            }
            //跑牌者增加息数
            PHZ_SuanXi::__xi( $room_info , $user_card_info['uid'] , "mop_open_" . $pao_type , $card_number , $db_connect );
            //给所有用户广播跑牌信息
            $temp = array();
            $temp['pl']          = 17;//17::摸牌后；摸牌者丢弃牌；同时有另一个玩家手里有3张相同的；满足跑牌要求
            $temp['card_number'] = $card_number;//因为是摸牌起跑，所有人都是能看到牌面的信息的
            $temp['put_uid']     = self::$user_mopai_chk_info[$uid]['chong_pao'] == 1 ? 0 : $user_card_info['uid'];//跑牌者非重跑情况下，自身出牌权；重跑则没有出牌权
            $temp['mop_uid']     = 0;//全部人丧失摸牌权
            $temp['is_chong']    = self::$user_mopai_chk_info[$uid]['chong_pao'];//是否是重跑
            $temp['pao_type']    = $pao_type;//起跑的类型？  "shou" "peng" "wei" - 前台通过这3种情况，进行不同的界面操作
            PHZ_Log::__send_user_log( $room_info , $user_card_info['uid'] , $temp  , $db_connect );
            //设置出牌权
            PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
            //如果是重跑，则当前记录重跑状态
            if( self::$user_mopai_chk_info[$uid]['chong_pao'] == 1 ) {
                self::$pao_paizhe_is_chongpao = true;
            }
            //返回true；代表跑被执行了
            return true;
        }else{
            return false;
        }
    }
}
























